#ifndef ENETWORK_H
#define ENETWORK_H
#pragma once
// #include "CommonUtils_global.h"
#include <QByteArray>
#include <QNetworkAccessManager>
#include <QNetworkReply>

class QJsonObject;

namespace ns_yht
{
#define TOKEN ENetwork::token

    class /*COMMONUTILS_EXPORT*/ ENetwork : QObject
    {
        Q_OBJECT

    public:
        ENetwork();

        static void Post(QNetworkAccessManager *manage, const QByteArray &content, const QString &strUrl, bool needAuthenticated = true);

        static QNetworkReply *Post(const QByteArray &content, const QString &strUrl, const QByteArray &httpTypeName = QByteArrayLiteral(""), const QByteArray &httpTypeValue = QByteArrayLiteral(""), bool needAuthenticated = true);
        static QNetworkReply *PostWithJson(const QByteArray &json, const QString &strUrl, const QByteArray &httpTypeName = QByteArrayLiteral(""), const QByteArray &httpTypeValue = QByteArrayLiteral(""), bool needAuthenticated = true);
        static QNetworkReply *PostFile(QMap<QString, QString> files, const QString &strUrl, const QByteArray &httpTypeName, const QByteArray &httpTypeValue, bool needAuthenticated = true);
        static QNetworkReply *Get(const QString &url, const QByteArray &httpTypeName = QByteArrayLiteral(""), const QByteArray &httpTypeValue = QByteArrayLiteral(""), bool needAuthenticated = true);
        static QNetworkReply *Delete(const QString &url, const QByteArray &httpTypeName, const QByteArray &httpTypeValue, bool needAuthenticated = true);
        static QNetworkReply *put(const QString &url, const QByteArray &httpTypeName, const QByteArray &httpTypeValue, bool needAuthenticated);
        // static QNetworkReply *doLogin(const QByteArray &deviceSerial);

        static QNetworkReply *Post(QNetworkAccessManager *manage, const QByteArray &content, const QString &strUrl, const QByteArray &httpTypeName = QByteArrayLiteral(""), const QByteArray &httpTypeValue = QByteArrayLiteral(""), bool needAuthenticated = true);
        static QNetworkReply *PostWithJson(QNetworkAccessManager *manage, const QByteArray &json, const QString &strUrl, const QByteArray &httpTypeName = QByteArrayLiteral(""), const QByteArray &httpTypeValue = QByteArrayLiteral(""), bool needAuthenticated = true);
        static QNetworkReply *PostFile(QNetworkAccessManager *manage, QMap<QString, QString> files, const QString &strUrl, const QByteArray &httpTypeName, const QByteArray &httpTypeValue, bool needAuthenticated = true);
        static QNetworkReply *Get(QNetworkAccessManager *manage, const QString &url, const QByteArray &httpTypeName = QByteArrayLiteral(""), const QByteArray &httpTypeValue = QByteArrayLiteral(""), bool needAuthenticated = true);
        static QNetworkReply *Delete(QNetworkAccessManager *manage, const QString &url, const QByteArray &httpTypeName, const QByteArray &httpTypeValue, bool needAuthenticated = true);
        static QNetworkReply *put(QNetworkAccessManager *manage, const QString &url, const QByteArray &httpTypeName, const QByteArray &httpTypeValue, bool needAuthenticated);
        // static QNetworkReply *doLogin(QNetworkAccessManager *manage, const QByteArray &deviceSerial);

        static bool checkReplyError(QNetworkReply *reply);

        /**
         * @brief 检查返回内容的根节点，看是否有错误
         *
         * @param root 根节点
         * @param remark 有错误要记录日志时的说明，主要时标志哪个http请求
         * @return true 有错误
         * @return false 没有错误
         */
        static bool checkRootError(const QJsonObject &root, const QString &remark = QString());

        static QString token;
        static bool networkStatus;

        friend class UpdateUtil;
        friend class MapUpdateUtil;

    private:
        static QNetworkAccessManager *getManager();
    };

} // namespace ns_yht

#endif // ENETWORK_H
